/*
* $Id: FamilyForm.java 272 2011-02-21 05:37:35Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui.journal;

import apiary.common.FormHelper;
import javax.microedition.lcdui.*;
        
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import apiary.Apiary;
import apiary.component.journal.Wizard;
import apiary.db.FamilyStore.FamilyRecord;
import apiary.db.PointStore.PointRecord;
import apiary.db.managers.DBManager;
import apiary.settings.SettingStore;
import apiary.settings.SettingsRecord;
import apiary.settings.SettingsRecord.Field;
import apiary.ui.CategoryItem;
import apiary.ui.ItemEventListener;

public class FamilyForm extends UserFieldForm implements ItemEventListener,
                                                         ItemStateListener
{
    public class FamilyValue
    {
        public int idPoint;
        public String nameFamily;
        public int typeFamily;
        public String noteFamily;
        public Vector userFields;

        public FamilyValue() {
            nameFamily = new String();
            noteFamily = new String();
            userFields = new Vector();
        }
    }

    private Command chooseCommand;
    private CategoryItem curCategory;
    
    public FamilyForm(String name, Integer idFrom, Wizard wiz, boolean nextCommand) {
        super(name, wiz, idFrom, nextCommand);
        chooseCommand = null;
        curCategory = null;
        Apiary apiary = Apiary.getInstance();
        append(apiary.getLocaleString("NamePoint") + "\n");
        CategoryItem item = FormHelper.createCategoryItem(this, "point", "", "");
        item.setState("0");
        append(item);
        append(new TextField(apiary.getLocaleString("NameFamily"), "", 256, TextField.ANY));
        Field field = (Field) SettingStore.getInstance().getData(SettingsRecord.TYPE_FAMILY);
        if(field.getValue() == Boolean.TRUE) {
            append(apiary.getLocaleString("TypeFamily") + "\n");
            item = FormHelper.createCategoryItem(this, "type", "", "");
            Hashtable types = DBManager.getInstance().getTypesFamily();
            item.setState(String.valueOf(FamilyRecord.TYPE_DEFAULT));
            item.setName((String) types.get(new Integer(FamilyRecord.TYPE_DEFAULT)));
            append(item);
        }
        field = (Field) SettingStore.getInstance().getData(SettingsRecord.NOTE_FAMILY);
        if(field.getValue() == Boolean.TRUE)
            append(new TextField(apiary.getLocaleString("Note"), "", 256, TextField.ANY));
        appendUserFields();

        setItemStateListener(this);
    }

    public void setFamilyValue(FamilyValue family) {
        int beginUser = 3;
        CategoryItem item = (CategoryItem) get(1);
        PointRecord pointRecord = DBManager.getInstance().getPoint(family.idPoint);
        item.setState(String.valueOf(pointRecord.idPoint));
        item.setName(pointRecord.namePoint);
        TextField text = (TextField) get(2);
        text.setString(family.nameFamily);
        SettingStore settings = SettingStore.getInstance();
        Field field = (Field) settings.getData(SettingsRecord.TYPE_FAMILY);
        if(field.getValue() == Boolean.TRUE) {
            item = (CategoryItem) get(beginUser + 1);
            Hashtable types = DBManager.getInstance().getTypesFamily();
            String type = (String) types.get(new Integer(family.typeFamily));
            item.setName(type);
            item.setState(String.valueOf(family.typeFamily));
            beginUser += 2;
        }
        field = (Field) settings.getData(SettingsRecord.NOTE_FAMILY);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            text.setString(family.noteFamily);
            beginUser++;
        }
        setUserFieldsValue(beginUser, family.userFields);
    }

    public Object GetDataForm() {
        int beginUser = 3;
        FamilyValue family = new FamilyValue();
        CategoryItem item = (CategoryItem) get(1);
        family.idPoint = Integer.parseInt(item.getState());
        TextField text = (TextField) get(2);
        family.nameFamily = text.getString();
        SettingStore settings = SettingStore.getInstance();
        Field field = (Field) settings.getData(SettingsRecord.TYPE_FAMILY);
        if(field.getValue() == Boolean.TRUE) {
            item = (CategoryItem) get(beginUser + 1);
            family.typeFamily = Integer.parseInt(item.getState());
            beginUser += 2;
        }
        field = (Field) settings.getData(SettingsRecord.NOTE_FAMILY);
        if(field.getValue() == Boolean.TRUE) {
            text = (TextField) get(beginUser);
            family.noteFamily = text.getString();
            beginUser++;
        }
        family.userFields = getUserFieldsValue(beginUser);
        return family;
    }

    protected void commandUser(Command command) {
        itemNotifyEvent(curCategory, PRESS);
    }

    public void itemNotifyEvent(Item item, String event) {
        if(event.equals(PRESS)){
            Apiary apiary = Apiary.getInstance();
            String name = "";
            CategoryItem categoryItem = (CategoryItem) item;
            if(categoryItem.getId().equals("point"))
                name = apiary.getLocaleString("ChoosePoint");
            else if(categoryItem.getId().equals("type"))
                name = apiary.getLocaleString("TypeFamily");
            ChooseList list = new ChooseList(name, item);
            if(categoryItem.getId().equals("point"))
                fillPointList(list);
            else if(categoryItem.getId().equals("type"))
                fillTypeList(list);
            list.setBackScreen(this);
            apiary.Display(list);
        }
    }

    public void itemStateChanged(Item item) {
        if(item.getClass() != CategoryItem.class)
            return;
        CategoryItem categoryItem = (CategoryItem) item;
        Apiary apiary = Apiary.getInstance();
        if(categoryItem.getActive()) {
            chooseCommand = new Command(apiary.getLocaleString("Choose"), Command.ITEM, 0);
            addCommand(chooseCommand);
            curCategory = categoryItem;
        } else {
            removeCommand(chooseCommand);
            curCategory = null;
        }
    }

    private void fillPointList(ChooseList list) {
        Vector points = DBManager.getInstance().getPoints();
        for(int i = 0; i < points.size(); i++) {
            PointRecord value = (PointRecord) points.elementAt(i);
            CategoryItem item = FormHelper.createCategoryItem(list,
                    String.valueOf(value.idPoint), value.namePoint, "");
            list.append(item);
        }
    }

    private void fillTypeList(ChooseList list) {
        Apiary apiary = Apiary.getInstance();
        Hashtable types = DBManager.getInstance().getTypesFamily();
        Enumeration typesEnum = types.keys();
        while(typesEnum.hasMoreElements()) {
            Integer type = (Integer) typesEnum.nextElement();
            String typeName = (String) types.get(type);
            CategoryItem item = FormHelper.createCategoryItem(list,
                    String.valueOf(type), typeName, "");
            list.insert(0, item);
        }
    }
}
